package com.dimanche.learnchinese;

import android.util.Log;

import androidx.databinding.ObservableField;
import androidx.lifecycle.Observer;

import com.dimanche.learnchinese.databinding.ActivityMainBinding;
import com.dimanche.library_base.baseui.BaseActivity;
import com.dimanche.library_base.utils.AppUtils;
import com.dimanche.library_base.utils.RouterUtil;
import com.dimanche.library_db.entity.WordEntity;
import com.dimanche.library_db.repository.DirRepository;
import com.dimanche.library_db.repository.UserRepository;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.hjq.toast.ToastUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;

public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";

    ActivityMainBinding binding;
    public ObservableField<String> users = new ObservableField<>();
    UserRepository userRepository;
    private final CompositeDisposable mDisposable = new CompositeDisposable();

    @Override
    public void initViews() {
        super.initViews();
        openImmersive();
//        userRepository = new UserRepository();
//        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
//        binding.setSize(users);
//        binding.insert.setOnClickListener(o -> {
//            UserEntity userEntity = new UserEntity();
//            userEntity.age = 1;
//            userEntity.count = "1234";
//            userEntity.name = "小花";
//            userEntity.paw = "5678";
//            userEntity.sex = 1;
//            mDisposable.add(userRepository.insert(userEntity).subscribe(aLong -> {
//                Log.i(TAG, "initViews: " + aLong);
//            }));
//        });
//        userRepository.queryByName("小明").observe(this, userEntities -> {
//            if (userEntities.size() > 0) {
//                users.set(userEntities.size() + "");
//            }
//        });

        //RouterUtil.launchHome();
        try {
            importDir();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入数据
     */
    private void importDir() throws UnsupportedEncodingException {
        DirRepository repository = DirRepository.getInstance();
        repository.queryAll().observe(this, new Observer<List<WordEntity>>() {
            @Override
            public void onChanged(List<WordEntity> wordEntities) {
                if (wordEntities.size() > 0) {
                    RouterUtil.launchCourse();
                    finish();
                    return;
                } else {
                    InputStream inputStream = AppUtils.getApplication().getResources().openRawResource(com.dimanche.library_db.R.raw.zidian6);
                    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
                    InputStreamReader reader = null;
                    try {
                        reader = new InputStreamReader(inputStream, "gbk");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    List<WordEntity> wordEntitys = gson.fromJson(reader, new TypeToken<List<WordEntity>>() {
                    }.getType());
                    for (WordEntity wordEntity2 : wordEntitys) {
                        Log.i("importDir", "汉字：" + wordEntity2.word);
                    }

                    mDisposable.add(repository.insert(wordEntitys).subscribe(lists -> {
                        ToastUtils.show(lists.size());
                        RouterUtil.launchCourse();
                        finish();
                    }));

                }

            }
        });

    }


}